package org.jizp.web.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jizp.web.bean.ResultBean;
import org.jizp.web.bean.XmlBean;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class XmlParseUtil {
	public final static String MVCMAPPING = "mvcMapping";

	public static void parseXml(String xmlPath, ServletContext servletContext) throws Exception {
		String realPath = servletContext.getRealPath(xmlPath);
		Map<String , String> map = new HashMap<String, String>();
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(realPath);
			Element element = document.getRootElement();
			List<Element> elements = element.elements();
			Map<String, XmlBean> mvcMapping = new HashMap<String, XmlBean>();
			for(Element el : elements){
				List<Attribute> attributes = el.attributes();
				for(Attribute attribute : attributes){
					if("base-package".equalsIgnoreCase(attribute.getName())){
						PackUtils.addClassAndMethodByAnnotation(attribute.getText(), mvcMapping);
					}
				}
			}
			servletContext.setAttribute(MVCMAPPING, mvcMapping);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
